package cn.cumulomq.service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import cn.cumulomq.MQXMain;
import cn.cumulomq.dao.AddressDao;
import cn.cumulomq.db.ConfigDB;
import cn.cumulomq.db.MessageDB;
import cn.cumulomq.entity.Address;
import cn.cumulomq.utils.FileUtil;
import cn.cumulomq.utils.PropertiesUtil;

/**
 * 管理AFTN和AMHS地址
 * @author maxuedong
 *
 */
public class AddressService {

	private Logger logger = Logger.getLogger(AddressService.class);
	
	/**
	 * 根据ID获取Address
	 * @param id
	 * @return
	 */
	public Address get(String id){
		Address addr = null;
		try{
			SqlSession session = ConfigDB.getSessionFactory().openSession();
			AddressDao addressDao = session.getMapper(AddressDao.class);
			addr = addressDao.get(id);
			session.close();
		}catch(Exception e){
			logger.error(e);
		}
		return addr;
	}
	/**
	 * 通过地址查询，如果存在多个则返回第一个
	 * @param address
	 * @return
	 */
	public Address queryByAddr(String address){
		Address addr = null;
		try{
			SqlSession session = ConfigDB.getSessionFactory().openSession();
			AddressDao addressDao = session.getMapper(AddressDao.class);
			addr = addressDao.queryByAddr(address);
			session.close();
		}catch(Exception e){
			logger.error(e);
		}
		return addr;
	}
	
	/**
	 * 保存地址，有ID为修改，没ID为新增
	 * @param address
	 * @return
	 */
	public boolean save(Address address){
		try{
			SqlSession session = ConfigDB.getSessionFactory().openSession();
			AddressDao addressDao = session.getMapper(AddressDao.class);
			if(address.getId()==null||"".equals(address.getId())){
				address.setId(UUID.randomUUID().toString());
				addressDao.insert(address);
				if(!Address.TYPE_AMHS.equals(address.getType())){
					if(!MQXMain.getInstance().addrEndpointMap.containsKey(address.getAddr())){
						MQXMain.getInstance().addrEndpointMap.put(address.getAddr(), MQXMain.getInstance().endpoints.get(address.getAftnChannelId()));
					}
				}else{
					//TODO: 连接AMHS
				}
			}else{
				addressDao.update(address);
				if(!Address.TYPE_AMHS.equals(address.getType())){
					//if(!MQXMain.getInstance().addrEndpointMap.containsKey(address.getAddr())){
						MQXMain.getInstance().addrEndpointMap.put(address.getAddr(), MQXMain.getInstance().endpoints.get(address.getAftnChannelId()));
					//}
					
				}else{
					//TODO: 连接AMHS
				}
			}
			session.commit();
			session.close();
		}catch(Exception e){
			logger.error(e);
			return false;
		}
//		createDatabase(address);
		return true;
	}

	/**
	 * 删除地址
	 * @param address
	 * @return
	 */
	public boolean delete(Address address){
		try{
			SqlSession session = ConfigDB.getSessionFactory().openSession();
			AddressDao addressDao = session.getMapper(AddressDao.class);
			addressDao.delete(address);
			MQXMain.getInstance().addrEndpointMap.remove(address.getAddr());
			session.commit();
			session.close();
		}catch(Exception e){
			logger.error(e);
			return false;
		}
		return true;
	}
	/**
	 * 获取地址列表
	 * @param address
	 * @return
	 */
	public List<Address> list(Address address){
		List<Address> res = new ArrayList<Address>();
		try{
			SqlSession session = ConfigDB.getSessionFactory().openSession();
			AddressDao addressDao = session.getMapper(AddressDao.class);
			res = addressDao.list(address);
			session.close();
		}catch(Exception e){
			logger.error(e);
		}
		return res;
	}
}
